﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using AEM.Utils;

namespace AEM.Data.DAL
{
    public static class ClientConfig
    {
        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Model.ClientConfig Search(int id)
        {
            return Select("ID", id.ToString());
        }

        private static Model.ClientConfig Select(string field, string value)
        {
            string condition = " " + field + "={0}";
            if (value.isNumeric())
                condition = string.Format(condition, value);
            else
                condition = string.Format(condition, "'" + value + "'");
            DataTable dt = Global.List("TableClientConfig",condition);
            if (null != dt && dt.Rows.Count > 0)
            {
                return Deserialize(dt.Rows[0]);
            }
            return null;
        }

        public static bool Add(Model.ClientConfig model)
        {
            string condition = @"INSERT INTO [TableClientConfig] (
                                    [ClientName], 
                                    [Address], 
                                    [Phone], 
                                    [Leader], 
                                    [LeadersPhone], 
                                    [SysBackgroud], 
                                    [UserLogo], 
                                    [ElevatorInput], 
                                    [EscalatorInput], 
                                    [WalkwayInput], 
                                    [ElevatorOutput], 
                                    [ID]) 
                                 VALUES ( 
                                    @ClientName, 
                                    @Address, 
                                    @Phone, 
                                    @Leader, 
                                    @LeadersPhone, 
                                    @SysBackgroud, 
                                    @UserLogo, 
                                    @ElevatorInput,
                                    @EscalatorInput, 
                                    @WalkwayInput,
                                    @ElevatorOutput,
                                    @ID)";

            int ret = AccessHelper.ExecuteNonQuery(condition, Param(model));
            if (-1 != ret) return true;
            return false;
        }

        public static bool Update(Model.ClientConfig model)
        {
            string condition = @"UPDATE  [TableClientConfig] SET
                                    [ClientName] = @ClientName, 
                                    [Address] = @Address, 
                                    [Phone] = @Phone, 
                                    [Leader] = @Leader, 
                                    [LeadersPhone] = @LeadersPhone, 
                                    [SysBackgroud] = @SysBackgroud, 
                                    [UserLogo] = @UserLogo, 
                                    [ElevatorInput] = @ElevatorInput, 
                                    [EscalatorInput] = @EscalatorInput, 
                                    [WalkwayInput] = @WalkwayInput, 
                                    [ElevatorOutput]= @ElevatorOutput 
                                WHERE  [ID] = @ID";

            int ret = AccessHelper.ExecuteNonQuery(condition, Param(model));
            if (-1 != ret) return true;
            return false;
        }

        private static OleDbParameter[] Param(Model.ClientConfig model)
        {
            List<OleDbParameter> param = new List<OleDbParameter>();
            param.Add(new OleDbParameter("@ClientName", model.ClientName));
            param.Add(new OleDbParameter("@Address", model.Address));
            param.Add(new OleDbParameter("@Phone", model.Phone));
            param.Add(new OleDbParameter("@Leader", model.Leader));
            param.Add(new OleDbParameter("@LeadersPhone", model.LeadersPhone));
            param.Add(new OleDbParameter("@SysBackgroud", model.SysBackgroud));
            param.Add(new OleDbParameter("@UserLogo", model.UserLogo));
            param.Add(new OleDbParameter("@ElevatorInput", model.ElevatorInput));
            param.Add(new OleDbParameter("@EscalatorInput", model.EscalatorInput));
            param.Add(new OleDbParameter("@WalkwayInput", model.WalkwayInput));
            param.Add(new OleDbParameter("@ElevatorOutput", model.ElevatorOutput));
            param.Add(new OleDbParameter("@ID", model.ID));
            return param.ToArray();
        }

        private static Model.ClientConfig Deserialize(DataRow dr)
        {
            Model.ClientConfig model = null;
            if (null != dr)
            {
                model = new Model.ClientConfig();
                model.ID = Convert.ToInt32(dr["ID"].ToString());
                model.ClientName = dr["ClientName"].ToString();
                model.Address = dr["Address"].ToString();
                model.Phone = dr["Phone"].ToString();
                model.Leader = dr["Leader"].ToString();
                model.LeadersPhone = dr["LeadersPhone"].ToString();
                model.SysBackgroud = dr["SysBackgroud"].ToString();
                model.UserLogo = dr["UserLogo"].ToString();
                model.ElevatorInput = dr["ElevatorInput"].ToString();
                model.ElevatorOutput = dr["ElevatorOutput"].ToString();
                model.EscalatorInput = dr["EscalatorInput"].ToString();
                model.WalkwayInput = dr["WalkwayInput"].ToString();
            }
            return model;
        }

        public static Model.ClientConfig Search()
        {
            DataTable dt = Global.List("TableClientConfig", "");
            if (null != dt && dt.Rows.Count > 0)
            {
                return Deserialize(dt.Rows[0]);
            }
            return null;
        }
    }
}
